package start.spring.basic.job.service.jobmonitor.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import start.spring.basic.constant.CommonConstant;
import start.spring.basic.job.common.JobConstant;
import start.spring.basic.job.dao.GgPzJobMapper;
import start.spring.basic.job.entity.GgPzJob;
import start.spring.basic.job.service.BaseServiceImpl;
import start.spring.basic.job.service.jobmonitor.IGgPzJobService;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.util.common.StringUtil;

/**
 * 定时任务Service业务层处理
 * 
 * @author xyy
 * @date 2023-06-26
 */
@Service
public class GgPzJobServiceImpl extends BaseServiceImpl implements IGgPzJobService {
    
    @Autowired
    private GgPzJobMapper ggPzJobMapper;
    
    @Value("${spring.application.name:job}")
    private String serviceId;

    /**
     * 查询定时任务列表
     * 
     * @param ggPzJob 定时任务
     * @return 定时任务
     */
    @Override
    public PageResult<GgPzJob> selectGgPzJobPageList(PageQuery pageQuery,GgPzJob ggPzJob) {
        IPage<GgPzJob> page = super.getPage(pageQuery);
        QueryWrapper<GgPzJob> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ggPzJob.getJobclass()!=null, GgPzJob::getJobclass, ggPzJob.getJobclass());
        queryWrapper.lambda().like(!StringUtil.isEmpty(ggPzJob.getJobname()), GgPzJob::getJobname, ggPzJob.getJobname());
    	queryWrapper.lambda().eq(!StringUtil.isEmpty(ggPzJob.getStatus()), GgPzJob::getStatus, ggPzJob.getStatus());
        if(!StringUtil.isEmpty(ggPzJob.getBeginKssj()) && !StringUtil.isEmpty(ggPzJob.getEndKssj())) {
        	queryWrapper.lambda().between(GgPzJob::getKssj, ggPzJob.getBeginKssj() + CommonConstant.SURFIX_DATE_BEGIN, ggPzJob.getEndKssj() + CommonConstant.SURFIX_DATE_END);
        }
    	queryWrapper.lambda().eq(!StringUtil.isEmpty(ggPzJob.getYxbz()), GgPzJob::getYxbz, ggPzJob.getYxbz());
    	queryWrapper.lambda().eq(!StringUtil.isEmpty(ggPzJob.getSfbf()), GgPzJob::getSfbf, ggPzJob.getSfbf());
        page = this.ggPzJobMapper.selectPage(page, queryWrapper);
        return super.getPageResult(page);
    }
    
    /**
     * 查询全量定时任务
     * 
     * @param ggPzJob 定时任务
     * @return 定时任务
     */
    @Override
    public List<GgPzJob> selectGgPzJobList(GgPzJob ggPzJob) {
        QueryWrapper<GgPzJob> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ggPzJob.getJobclass()!=null, GgPzJob::getJobclass, ggPzJob.getJobclass());
        queryWrapper.lambda().like(!StringUtil.isEmpty(ggPzJob.getJobname()), GgPzJob::getJobname, ggPzJob.getJobname());
    	queryWrapper.lambda().eq(!StringUtil.isEmpty(ggPzJob.getStatus()), GgPzJob::getStatus, ggPzJob.getStatus());
        if(!StringUtil.isEmpty(ggPzJob.getBeginKssj()) && !StringUtil.isEmpty(ggPzJob.getEndKssj())) {
        	queryWrapper.lambda().between(GgPzJob::getKssj, ggPzJob.getBeginKssj() + CommonConstant.SURFIX_DATE_BEGIN, ggPzJob.getEndKssj() + CommonConstant.SURFIX_DATE_END);
        }
    	queryWrapper.lambda().eq(!StringUtil.isEmpty(ggPzJob.getYxbz()), GgPzJob::getYxbz, ggPzJob.getYxbz());
    	queryWrapper.lambda().eq(!StringUtil.isEmpty(ggPzJob.getSfbf()), GgPzJob::getSfbf, ggPzJob.getSfbf());
        return this.ggPzJobMapper.selectList(queryWrapper);
    }
    
    /**
     * 修改定时任务
     * 
     * @param ggPzJob 定时任务
     * @return 结果
     */
    @Override
    public int updateGgPzJob(GgPzJob ggPzJob) {
    	UpdateWrapper<GgPzJob> updateWrapper = new UpdateWrapper<>();
    	updateWrapper.lambda().eq(GgPzJob::getJobclass, ggPzJob.getJobclass());
    	int updateCount = this.ggPzJobMapper.update(ggPzJob, updateWrapper);
    	
    	//更新到数据，则redis缓存更新记录，用于更新已加载到内存的定时任务
    	if(updateCount>0) {
    		String key = JobConstant.JOB_UPDATE_PREFIX + ggPzJob.getJobclass();
    		String value = String.valueOf(System.currentTimeMillis());
        	super.redis.setex(null, key, value, JobConstant.JOB_UPDATE_CACHETIME);
    	}
        return updateCount;
    }
}
